/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.pojo.Disciplina;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

/**
 * 
 * @author baron
 */
public class frmDisciplina extends FrmModelo {
	Disciplina disciplina;
	StateHandler stateAlterarExcuir = new StateHandler(false);
	public List<Object> campos = ClientCommand.getCampos("DisciplinaService");// new/ ArrayList<Object>();
	private List<Object> valores = new ArrayList<Object>();
	private List<Object> tamanho = new ArrayList<Object>();
	TabelaModelo tabModel;
	List<Disciplina> extra;

	public frmDisciplina(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		tamanho.add("70");
		tabModel = new TabelaModelo(campos, tamanho, getJTable());
        tabModel.busca(getTfBuscar());
		setTitle("Disciplina");
		ListSelectionModel listModel = getJTable().getSelectionModel();
		listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listModel.addListSelectionListener(list);
        setStateAlteraExcuir();
		setButtons();
		atualiza();
		setVisible(true);
	}

	public void setButtons() {
		JButton btn4 = getbtn4();
		JButton btn5 = getbtn5();
		JButton btn6 = getbtn6();

		btn4.setVisible(false);
		btn5.setVisible(false);
		btn6.setVisible(false);
		
		getbtnVoltar().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		getJbtnGravar().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new frmCadastroDisciplina();
			}
		});
		getbtnAlterar().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					new frmAtualizarDisciplina(disciplina);
					atualiza();
				} catch (NullPointerException e) {
					JOptionPane.showMessageDialog(null,
							"seleciona a disciplina que deseja alterar");
				}
			}
		});
		getbtnExcluir().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					for (int i = 0; i < extra.size(); i++) {
						if (extra.get(i).getRequisito().getId() == disciplina
								.getId())
							disciplina = null;
					}ClientCommand.deletar("DisciplinaService", disciplina);
					atualiza();
				} catch (NullPointerException e) {
					JOptionPane.showMessageDialog(null,
							"seleciona a disciplina que deseja excluir, ela nao pode ser "
									+ "requisitada por outra disciplina");
				}
			}
		});
	}

	public void atualiza() {
		((DefaultTableModel) getJTable().getModel()).setNumRows(0);
		getJTable().updateUI();
		List carregar = (List) ClientCommand.carregar("DisciplinaService",
				"codigo = codigo", "");
		extra = carregar;
		for (int i = 0; i < extra.size(); i++) {
			valores = new LinkedList<Object>();
			valores.add(extra.get(i).getId());
			valores.add(extra.get(i).getNome());
			if (extra.get(i).getRequisito().getId() == 0)
				valores.add("Nenhum");
			else {
				extra.get(i).setRequisito(
						(Disciplina) ClientCommand.carregaObjeto(
								"DisciplinaService", "codigo = "
										+ extra.get(i).getRequisito().getId(),
								""));
				valores.add(extra.get(i).getRequisito().getNome());
			}
			// valores.add(extra.get(i).getRequisito().getId());
			valores.add(extra.get(i).getCargaHoraria());
			if(extra.get(i).getStatus()==1)
			valores.add("Regulamentado");				
			tabModel.setValores(valores);
			valores.clear();
		}
	}

	ListSelectionListener list = new ListSelectionListener() {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			try {
				disciplina = getSelectedDisciplina(Long
						.parseLong(getJTable().getValueAt(
								getJTable().getSelectedRow(), 0).toString()));
				stateAlterarExcuir.change(true);
			} catch (ArrayIndexOutOfBoundsException ea) {
				disciplina = null;
				stateAlterarExcuir.change(false);
			}
		}
	};

	public Disciplina getSelectedDisciplina(Long codigo) {
		return (Disciplina) ClientCommand.carregaObjeto("DisciplinaService", "codigo ="+codigo, "");
	}
    public void setStateAlteraExcuir(){
    	stateAlterarExcuir.addComponent(getbtnAlterar());
    	stateAlterarExcuir.addComponent(getbtnExcluir());
    }
}
